layui.use(['laydate','form','table','laytpl','layer'], function(){
	var laydate = layui.laydate;
	var form = layui.form;
	var table = layui.table;
	var laytpl = layui.laytpl;
	var layer = layui.layer;
	layui.cate = [1,2,3];//采购支出分类集合
	var cate_id = $('select[name="expend_cate_id"]').val()
	/*@时间*/
	laydate.render({
	  	elem: '#start' //指定元素
	});
	laydate.render({
	  	elem: '#end' //指定元素
	});

	let type = $('.activity-btn').attr('lay-event')
	/*@ss*/
	cate_table = function (){
		console.log(cate_id)
		if($.inArray(Number(cate_id), layui.cate) >= 0){/*是采购支出*/
			return [[ //表头
		    	{field: 'expend_id', title: '序号'},
		    	{field: 'addtime', title: '支出时间'},
		    	{field: 'cat_name', title: '支出类型'},
		    	{field: 'contract_sn', title: '采购编号'},
		    	{field: 'goods_name', title: '产品名称'},
		    	{field: 'cat_f_name', title: '产品分类'},
		    	{field: 'cat_s_name', title: '产品子分类'},
		    	{field: 'brand_name', title: '品牌'},
		    	{field: 'marker', title: '型号'},
		    	{field: 'memory_name', title: '内存'},
		    	{field: 'attribute_name', title: '属性'},
		    	{field: 'colour_name', title: '颜色'},
		    	{field: 'number', title: '数量'},
		    	{field: 'unit', title: '单位'},
		    	{field: 'buying_price', title: '进货单价（元）'},
		    	{field: 'supplier_name', title: '供应商'},
		    	{field: 'money', title: '支出总额（元）'},
		    	{title: '操作', toolbar: '#table-btn'}
		    ]]
		}else{
			return [[ //表头
		    	{field: 'expend_id', title: '序号'},
		    	{field: 'addtime', title: '支出时间'},
		    	{field: 'cat_name', title: '支出类型'},
		    	{field: 'explains', title: '支出说明'},
		    	{field: 'money', title: '支出总额（元）'},
		    	{title: '操作', toolbar: '#table-btn'}
		    ]]
		}
	}
	/*@初始化table*/
	table.render({
	    elem: '#table-list',
	    id: 'list',
	    url: '/finance/pay/index', //数据接口
	    page: true, //开启分页
	    where: {expend_cate_id: cate_id},
	    cols: cate_table(),
	    parseData: function(res){
	    	let coun = res.data.count
	    	res.data = res.data.list
	    	let getTpl_2 = oper_box.innerHTML;
			let count = document.getElementById('oper-box');
			laytpl(getTpl_2).render(coun, function(html){
				count.innerHTML = html;
			});
	    }
	});

	// /*@查询*/
	form.on('submit(search)',function(data){
		cate_id = data.field.expend_cate_id
		/*表格数据初始化*/
        table.render({
		    elem: '#table-list',
		    id: 'list',
		    url: '/finance/pay/index', //数据接口
		    page: true, //开启分页
		    where: data.field,
		    cols: cate_table(),
		    parseData: function(res){
		    	let coun = res.data.count == 0 ? '0' : res.data.count
		    	res.data = res.data.list
		    	let getTpl_2 = oper_box.innerHTML;
				let count = document.getElementById('oper-box');
				laytpl(getTpl_2).render(coun, function(html){
					count.innerHTML = html;
				});
		    }
		});
        return false
	})
	/*@监听工具*/
	table.on('tool(list)', function(obj_tr){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
	  	var data = obj_tr.data; //获得当前行数据
	  	var layEvent = obj_tr.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
	  	var tr = obj_tr.tr; //获得当前行 tr 的DOM对象
	  	if(layEvent === 'del'){ //删除
	  		layer.confirm('确定删除', {icon: 3, title:'提示'}, function(){
	  			let obj_data = {}
		  		obj_data.id = data.expend_id
		  		del(obj_data)
	  		})
	  		
	  	}
	});
	/*@删除*/
	function del(obj_data){
		$.post('/finance/pay/del', obj_data, function(data){
			if(data.code !== 1){
				layer.msg(data.msg, {icon: 5});
			}else{
				layer.msg(data.msg, {icon: 6, time: 1000}, function(){
					table.reload('list')
				})
			}
		})
	}
});